// 2025/4/5
// https://leetcode.cn/problems/powx-n/
// Pow(x,n)

class Solution {
    public:
        double myPow(double x, int n) {
            double ans = 1, tmp = x;
            int k = n;
            if (n < 0)
            {
                tmp = 1 / x;
                k = ((~k) | 0x80000000) + 1;
            }
            for (int i = 0; i < 31; i++)
            {
                if ((k >> i) & 1 || n == INT_MIN)
                    ans *= tmp;
                tmp = tmp * tmp;
            }
            if(n == INT_MIN) ans *= x;
            return ans;
        }
    };